package com.fbinggun.playeraddattribute;

import com.fbinggun.fattribute.attribute.AttributeManage;
import com.fbinggun.fattribute.attribute.IAttribute;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Command implements CommandExecutor, TabExecutor {
    @Override
    public boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String label, String[] args) {
        if(!sender.isOp()){
            return false;
        }

        if(args.length==0){
            return false;
        }
        if(args[0].equalsIgnoreCase("add")){
            String player =args[1];
            String key = args[2];
            Double value = Double.valueOf(args[3]);
            IAttribute iAttribute = IAttribute.getKey(key);
            if(iAttribute==null){
                sender.sendMessage("无此属性");
                return false;
            }
            try {
                PlayerData playerData = PlayerAddAttribute.plugin.getPlayerData(player) ;
                playerData.add(key,value);
                sender.sendMessage("添加成功");
                Player player1 = Bukkit.getPlayer(player);
                if(player1!=null){
                    player1.sendMessage("§b[属性系统]: &d"+key+" 属性值增加了"+value+"当前属性值: "+playerData.getAttribute(key));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            return false;
        }
        if(args[0].equalsIgnoreCase("set")){
            String player =args[1];
            String key = args[2];
            Double value = Double.valueOf(args[3]);
            IAttribute iAttribute = IAttribute.getKey(key);
            if(iAttribute==null){
                sender.sendMessage("无此属性");
                return false;
            }
            try {
                PlayerData playerData = PlayerAddAttribute.plugin.getPlayerData(player) ;
                playerData.set(key,value);
                sender.sendMessage("添加成功");
                Player player1 = Bukkit.getPlayer(player);
                if(player1!=null){
                    player1.sendMessage("§b[属性系统]: &d"+key+" 当前属性值: "+playerData.getAttribute(key));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            return false;
        }

        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, org.bukkit.command.Command command, String alias, String[] args) {
        List<String> list = new ArrayList<>();
        if(args.length==1){
            list.add("add");
            list.add("set");
            return list;
        }
        if(args.length==2){
            for(Player player:Bukkit.getOnlinePlayers()){
              list.add( player.getName());
            }
            return list;
        }
        if(args.length==3){
            for(String str: AttributeManage.getAttributeMap().keySet()){
                list.add( str);
            }
            return list;
        }
        if(args.length==4){
           list.add("1");
            list.add("10");
            return list;
        }

        return null;
    }
}
